package models

import (
	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

type SaCollectfeesDispensing struct {
	Id           int64          `json:"id"`
	Archives     *Archives      `orm:"rel(fk);null;on_delete(do_nothing)" json:"archives"`
	DepartmentId int64          `json:"department_id"`
	Doctororder  *DoDoctorOrder `orm:"rel(fk);null;on_delete(do_nothing)" json:"doctororder"`
	CreatedAt    int64          `json:"created_at"`
	CreatedBy    string         `json:"created_by"`
}

func (a *SaCollectfeesDispensing) TableName() string {
	return SaCollectfeesDispensingTableName()
}

func SaCollectfeesDispensingTableName() string {
	return TableName("sa_collectfees_dispensing")
}

//查询
type SaCollectfeesDispensingQueryParam struct {
	BaseQueryParam
	Filter       string `json:"filter"` //姓名查询
	StartDate    string `json:"start_date"`
	EndDate      string `json:"end_date"`
	DepartmentId int64  `json:"department_id"`
}

//查询分页列表数据
func SaCollectfeesDispensingPageList(params *SaCollectfeesDispensingQueryParam) ([]*SaCollectfeesDispensing, int64) {

	query := orm.NewOrm().QueryTable(SaCollectfeesDispensingTableName()).RelatedSel("Archives", "Doctororder")
	data := make([]*SaCollectfeesDispensing, 0)

	cond := orm.NewCondition();
	if len(params.Filter) > 0 {
		cond1 := orm.NewCondition()
		cond = cond.AndCond(cond1.Or("Archives__pinyin__contains", params.Filter).Or("Archives__realname__contains", params.Filter).Or("Archives__mobile__contains", params.Filter))
	}
	query = query.SetCond(cond)
	if len(params.StartDate) > 0 {
		query = query.Filter("created_at__gte", utils.GetDateParse(params.StartDate))
	}
	if len(params.EndDate) > 0 {
		query = query.Filter("created_at__lte", utils.GetDateParse(params.EndDate)+86400)
	}
	query = query.Filter("department_id", params.DepartmentId)
	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}
	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&data)

	return data, total

}